package com.foreknow.web;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Department;
import com.foreknow.entity.RegistLevel;
import com.foreknow.entity.Rule;
import com.foreknow.service.RuleService;
import com.foreknow.utils.Constance;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Api
@RestController
@RequestMapping(value = "RuleController")
public class RuleController {
    @Autowired
    private RuleService ruleService;

    @RequestMapping(value = "/queryRuleForAll")
    public String queryRuleForAll() throws Exception {
        List<Rule> list = ruleService.queryRuleForAll();
        HashMap<String, Object> hs = new HashMap<>();


        hs.put("data", list);
        hs.put("meta", new Constance("msg", 200));

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


    //    暂时没用的的方法
    @RequestMapping(value = "/queryRuleByCondition")
    public String queryRuleByCondition(@RequestBody Map<String, Object> para) throws Exception {

        Integer deptId = (Integer) para.get("deptId");

        List<Rule> list = ruleService.queryRuleByCondition(deptId);
        HashMap<String, Object> hs = new HashMap<>();


        hs.put("data", list);
        hs.put("meta", new Constance("msg", 200));

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/selectForRuleListPage")
    public String selectForRuleListPage(@RequestBody Map<String, Object> para) throws Exception {


        Integer pageSize = (Integer) para.get("pageSize");
        Integer currentPage = (Integer) para.get("currentPage");

        Integer deptId = (Integer) para.get("deptId");

        Integer count = ruleService.countOfDept(deptId);
        List<Rule> list = ruleService.selectForRuleListPage(deptId, pageSize, currentPage);
        HashMap<String, Object> hs = new HashMap<>();

        hs.put("count", count);
        hs.put("data", list);
        hs.put("meta", new Constance("msg", 200));

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


    @RequestMapping(value = "addRule")
    public String addRule(@RequestBody Map<String, Object> para) throws Exception {
        String rulename = (String) para.get("rulename");
        Integer deptid = (Integer) para.get("deptId");
        List<String> week = (List<String>) para.get("week");
        List<Integer> userId = (List<Integer>) para.get("userId");

        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < week.size(); i++) {
            Rule rule = new Rule();
            rule.setDeptid(deptid);
            rule.setWeek(week.get(i));
            rule.setUserid(userId.get(i));
            rule.setRulename(rulename);
            rule.setDelmark(1);
            int jjj = ruleService.addRule(rule);
            list.add(jjj);
        }

        HashMap<String, Object> hs = new HashMap<>();


        hs.put("data", list);
        hs.put("meta", new Constance("msg", 200));

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "delRule")
    public String delRule(@RequestBody Map<String, Object> para) throws Exception {

        List<Integer> ruleId = (List<Integer>) para.get("list");

        ruleService.deleteRule(ruleId);

        HashMap<String, Object> hs = new HashMap<>();


        hs.put("meta", new Constance("msg", 200));

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


    @RequestMapping(value = "editRule")
    public String editRule(@RequestBody Map<String, Object> para) throws Exception {
        Integer id = (Integer) para.get("id");
        String week = (String) para.get("week");
        Integer userId = (Integer) para.get("userId");
        Rule rule = new Rule();
        rule.setWeek(week);
        rule.setId(id);
        rule.setUserid(userId);
        int i = ruleService.updateRule(rule);

        HashMap<String, Object> hs = new HashMap<>();

        if (i > 0) {
            hs.put("meta", new Constance("msg", 200));
        }

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

}
